﻿using Bll;
using DecorationSys.Models;
using Entity;
using Entity.DTO;
using IBll;
using Microsoft.AspNetCore.Mvc;

namespace DecorationSys.Controllers
{
    /// <summary>
    /// 通知相关
    /// </summary>
    public class NoticeController : Controller
    {
        INoticeBll _noticeBll;

        public NoticeController(INoticeBll noticeBll)
        {
            _noticeBll = noticeBll;
        }

        /// <summary>
        /// 通知列表视图
        /// </summary>
        /// <returns></returns>
        public IActionResult NoticeListView()
        {
            return View();
        }

        /// <summary>
        /// 添加通知视图
        /// </summary>
        /// <returns></returns>
        public IActionResult CreateNoticeView()
        {
            return View();
        }

        /// <summary>
        /// 修改通知视图
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdateNoticeView()
        {
            return View();
        }

        /// <summary>
        /// 通知详情视图
        /// </summary>
        /// <returns></returns>
        public IActionResult DetailsView()
        {
            return View();
        }

        /// <summary>
        /// 根据通知id获取通知信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public MyJsonResult GetNoticeById(string id)
        {
            var Notice = _noticeBll.GetNoticeById(id);

            if (Notice == null)
            {
                return MyJsonResult.GetErrorResult("通知不存在");
            }
            else
            {
                return MyJsonResult.GetSuccessResult(Notice);
            }
        }

        /// <summary>
        /// 更新通知
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult UpdateNotice(string id, string title, string content)
        {
            //参数验证
            if (string.IsNullOrEmpty(id))
            {
                return MyJsonResult.GetErrorResult("通知id不能为空");
            }
            if (string.IsNullOrEmpty(title))
            {
                return MyJsonResult.GetErrorResult("通知名不能为空");
            }

            string msg;
            //调用业务逻辑层
            bool isSuccess = _noticeBll.UpdateNotice(id, title, content ,out msg);
            if (isSuccess)
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult(msg);
            }
        }


        /// <summary>
        /// 获取通知列表数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public object GetNoticeList(string title, int page, int limit)
        {
            if (page <= 0)
            {
                return new
                {
                    code = 0,
                    msg = "页码有误",
                    count = 0,
                    data = new List<object>()
                };
            }

            if (limit <= 0)
            {
                return new
                {
                    code = 0,
                    msg = "每页条数有误",
                    count = 0,
                    data = new List<object>()
                };
            }

            int count;

            //查通知数据
            var list = _noticeBll.GetNoticeList(title, page, limit, out count);

            return new
            {
                code = 0,
                msg = "cg",
                count,
                data = list
            };
        }

        /// <summary>
        /// 创建通知
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult CreateNotice(string title, string content)
        {
            //参数验证
            if (string.IsNullOrEmpty(title))
            {
                return MyJsonResult.GetErrorResult("请输入通知标题");
            }

            if (string.IsNullOrEmpty(content))
            {
                return MyJsonResult.GetErrorResult("请输入通知内容");
            }

            string userId = HttpContext.Session.GetString("UserId");

            //用户id有值的时候才去数据库查菜单数据
            if (string.IsNullOrEmpty(userId))
            {
                return MyJsonResult.GetErrorResult("请重新登录");
            }



            string msg;
            bool isSuccess = _noticeBll.CreateNotice(title, content, userId, out msg);

            if (isSuccess)
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult(msg);
            }
        }

        /// <summary>
        /// 软删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult DeleteNotice(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return MyJsonResult.GetErrorResult("参数有误");
            }

            bool isSuccess = _noticeBll.DeleteNotice(id);

            if (isSuccess)
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult("失败");
            }
        }

        /// <summary>
        /// 批量软删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult DeleteNotices(List<string> ids)
        {
            if (ids.Count <= 0 || ids == null)
            {
                return MyJsonResult.GetErrorResult("参数有误");
            }

            bool isSuccess = _noticeBll.DeleteNotices(ids);

            if (isSuccess)
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult("失败");
            }
        }

    }

}
